package edu.tec.mty.test.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.util.HashMap;
import javax.swing.*;

class InfoBox {
	
	private JPanel parentPanel;
	private JPanel childPanel;
	private HashMap<String, JTextField> labels;
	private String name;
	private Font font;
	
	public InfoBox(String name) {
		this.name = name;
		this.labels = new HashMap<String, JTextField>();
		this.parentPanel = new JPanel(new BorderLayout());
		this.parentPanel.setBorder(BorderFactory.createTitledBorder(name));
		this.childPanel = this.parentPanel;
		this.font = new Font("Arial", 0, 11);
	}
	
	public void addLabel(String name) {
		JPanel panel = new JPanel(new BorderLayout());
		String labelText = name;
		if (labelText.length() > 13) {
			labelText = labelText.substring(0,11) + ".";
		} else {
			while(labelText.length() < 11) {
				labelText = labelText + " ";
			}
		}
		JLabel label = new JLabel(labelText);
		label.setFont(this.font);
		panel.add(label, BorderLayout.WEST);
		JTextField textField = new JTextField("-",13);
		textField.setEditable(false);
		panel.add(textField, BorderLayout.EAST);
		this.labels.put(name, textField);
		this.childPanel.add(panel, BorderLayout.NORTH);
		JPanel nextPanel = new JPanel(new BorderLayout());
		this.childPanel.add(nextPanel, BorderLayout.SOUTH);
		this.childPanel = nextPanel;
	}
	
	public void setValue(String label, String value) throws Exception {
		if (this.labels.containsKey(label)) {
			this.labels.get(label).setText(value);
		} else {
			throw new Exception("The label doesnt exist");
		}
	}
	
	public JPanel getPanel() {
		return this.parentPanel;
	}
	
}

public class InfoBoxes {

	private HashMap<String, InfoBox> infoBoxes;
	private JPanel parentPanel;
	private JPanel childPanel;
	
	public InfoBoxes() {
		this.infoBoxes = new HashMap<String, InfoBox>();
		this.parentPanel = new JPanel(new BorderLayout());
		this.childPanel = this.parentPanel;
	}
	
	public void addInfoBox(String name) {
		InfoBox infoBox = new InfoBox(name);
		this.infoBoxes.put(name, infoBox);
		this.childPanel.add(infoBox.getPanel(), BorderLayout.WEST);
		JPanel nextPanel = new JPanel(new BorderLayout());
		this.childPanel.add(nextPanel, BorderLayout.EAST);
		this.childPanel = nextPanel;
	}
	
	public void addLabel(String infoBox, String labelName) throws Exception {
		if (this.infoBoxes.containsKey(infoBox)) {
			this.infoBoxes.get(infoBox).addLabel(labelName);
		} else {
			throw new Exception("The infoBox doesnt exist");
		}
	}
	
	public void setValue(String infoBox, String label, String value) throws Exception {
		if (this.infoBoxes.containsKey(infoBox)) {
			this.infoBoxes.get(infoBox).setValue(label, value);
		} else {
			throw new Exception("The infoBox doesnt exist");
		}
	}
	
	public JPanel getPanel() {
		return this.parentPanel;
	}
	
}
